package com.huang.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.entity.UserPo;
import com.huang.enums.UserCodeEnum;
import com.huang.exception.BizException;
import com.huang.mapper.UserDao;
import com.huang.resp.CommonResult;
import com.huang.service.UserService;

import cn.hutool.core.util.StrUtil;

/**
 * @author han
 * @since 2021-10-12 19:47:49
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserDao, UserPo> implements UserService {
	
	@Resource
	private UserDao userDao;

	@Override
	public boolean saveUserPro(UserPo userPo) {
		checkPhone(userPo);
		return this.save(userPo);
	}

	@Override
	public boolean updateByUser(UserPo userPo) {
		checkPhone(userPo);
		return this.updateById(userPo);
	}
	
	// 判断手机号码
	private void checkPhone(UserPo userPo) {
		LambdaQueryWrapper<UserPo> queryWrapper = new LambdaQueryWrapper<UserPo>();
		List<UserPo> list = userDao.selectList(queryWrapper
				.eq(UserPo::getPhone, userPo.getPhone())
				.ne(StrUtil.isNotBlank(userPo.getId()), UserPo::getId, userPo.getId())
				);
		if (null != list && list.size() > 0) {
			throw new BizException(CommonResult.error(UserCodeEnum.ACCOUNT_EXIST));
		}
	}


}
